package action.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import persistence.model.TdfkfaUser;
import presentation.PageVO;
import service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class ManagePlanUserAction extends ActionSupport implements
		ServletRequestAware {

	private static final long serialVersionUID = 2782173936481360467L;

	private HttpServletRequest request;

	private UserService userService;

	private String[] userId;

	private String uid;

	private TdfkfaUser planUser;

	private String status;

	private List<TdfkfaUser> planUserList;

	private PageVO pageVO;

	public String list() throws Exception {

		pageVO.setRecordNum(userService.countUserByAuthority(new Integer("4")));

		planUserList = userService.getUserListByAuthority(new Integer("4"), pageVO.getIntPageIndex(), pageVO.getIntPageSize());

		return SUCCESS;
	}

	public String toEditUser() throws Exception {
		if (userId == null || userId.length != 1) {
			return INPUT;
		} else {
			uid = userId[0];
			request.getSession().setAttribute("uid", uid);
			return SUCCESS;
		}
	}

	public String prepareEditUser() throws Exception {
		uid = (String) request.getSession().getAttribute("uid");
		planUser = userService.getUser(new Integer(uid));

		if (planUser == null) {
			return INPUT;
		}

		return SUCCESS;
	}

	public String modifyUser() throws Exception {

		planUser.setUsername(null);
		planUser.setCreateDate(null);
		int r = userService.updateUser(planUser);
		if (r == 1) {
			status = "success";
		} else {
			status = "failure";
		}
		return SUCCESS;
	}

	public String deleteUser() throws Exception {

		if (userId == null || userId.length == 0) {
			return INPUT;
		}

		Integer[] ids = new Integer[userId.length];
		for (int i = 0; i < userId.length; i++) {
			ids[i] = new Integer(userId[i]);
		}

		userService.deleteUser(ids);

		return SUCCESS;
	}

	public String[] getUserId() {
		return userId;
	}

	public void setUserId(String[] userId) {
		this.userId = userId;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public TdfkfaUser getPlanUser() {
		return planUser;
	}

	public void setPlanUser(TdfkfaUser planUser) {
		this.planUser = planUser;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getStatus() {
		return status;
	}

	public List<TdfkfaUser> getPlanUserList() {
		return planUserList;
	}

	public PageVO getPageVO() {
		return pageVO;
	}

	public void setPageVO(PageVO pageVO) {
		this.pageVO = pageVO;
	}

}
